<?php

namespace App\Http\Resources;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\Resource;

class Data extends Resource
{
    private $message = 'success';
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        if(method_exists($this, $request->method) && $method = $request->method){
            return $this->$method();
        }
        $this->status = 'failed';
        $this->code = '404';
    }

    public function getItems(){
        return [
            'id'=>$this->id,
            'title'=>$this->title,
            'image'=>env('APP_IMGURL').'/model/'.$this->image,
            'status'=>$this->status,
            'note'=>$this->note,
            'is_vip'=>$this->is_vip,
            'type'=>$this->type,
            'created_at'=>$this->created_at->toDateString(),
//            'update'=> (new Carbon())->diffInHours(Carbon::parse($this->published_at))<24?1:0,
            'update'=> 0,
        ];
    }
    //用户数据列表
    public function getusershow(){
        return [
            'id'=>$this->id,
            'title'=>$this->title,
            'type'=>$this->type,
            'image'=>env('APP_IMGURL').'/model/'.$this->image,
            'month'=>$this->month,
        ];
    }
    //模型详情
    public function getInfo(){
        return [
            'id' => $this->id,
            'type' => $this->type,
            'user_id' => $this->user_id,
            'title' => $this->title,
            'coins'=>$this->coins,
            'filesize'=>$this->filesize,
            'images'=>$this->images_thumb,
            'appversion'=>$this->appversion,
            'renderertype'=>$this->renderertype,
            'cat_id1'=>new Cat($this->catid1),
            'cat_id2'=>new Cat($this->catid2),
            'style'=>new Cat($this->cstyle),
            'is_light'=>$this->is_light,
            'is_vip'=>$this->is_vip,
            'status'=>$this->status,
            'viewnum'=>$this->numberformat($this->viewnum),
            'collecnum'=>$this->numberformat($this->collecnum),
            'downnum'=>$this->numberformat($this->downnum),
            'describe'=>$this->describe,
            'created_at'=>$this->created_at->toDateString(),
            'userinfo' => new User($this->user),
            'update'=> 0,
        ];
    }

    public function with($request){
        if($request->user_id){
            return  [
                'status' => $this->status,
                'message'   => $this->message,
                'userinfo' => new User(\App\User::find($request->user_id)),
            ];
        }else{
            return [
                'status' => 1,
                'message'  => $this->message,
            ];
        }
    }

    public function  numberformat($value){
        if($value < 999){
            return $value;
        }elseif($value < 99999){
            return number_format($value/1000, 1).'K';
        }else{
            return '10W+';
        }
    }

}
